import { h } from "vue";
import { ElNotification, NotificationOptions } from "element-plus";
import { merge } from "lodash-es";
import NotificationContent from "./index.vue";
import { useAlertDetailStore } from "@/stores/alertDeail";
import { getAlertMessageDetail } from "@/api/alert";
import {
  addDialog,
  closeDialog,
  closeAllDialog,
  updateDialog,
} from "@/components/redialog";
import { getEqDetails } from "@/api/equipment";
const alertDetailStore = useAlertDetailStore();
export const Notification = (data: any = {}, options?: NotificationOptions) => {
  setTimeout(() => {
    let instance: any = null;
    const mergeOptions = merge(
      {
        duration: 0,
        position: "top-right",
        // offset: 48,//y轴位置偏移量
        message: h(NotificationContent, { ...data }), //创建HTML片段
        onClick: () => {
          const testclose = (options, index) => {
            return () => closeDialog(options, index);
          };
          //添加点击事件
          if (instance) {
            console.log(data.alertMessageId);

            getAlertMessageDetail(data.alertMessageId).then((res) => {
              getEqDetails(res.data.businessId).then((res1) => {
                let alertMessage = {
                  equipmentId: res.data.businessId,
                  alertMessageId: res.data.id,
                  alertContent: res.data.alertContent,
                  alertGrade: res.data.alertGrade,
                  handleStatus: res.data.handleStatus,
                  imgFlag: res.data.imgFlag,
                  equipmentName: res.data.equipmentName,
                  equipmentType: res.data.equipmentType,
                  equipmentLocation: res1.data.equipmentLocation,
                  imageUrl: res.data.imageUrl,
                  timeOfDuration: res.data.timeOfDuration,
                  equipmentLocationCode: res1.data.equipmentLocationCode
                    ? res1.data.equipmentLocationCode
                    : "",
                  alertToneUrl: null,
                  latestAlertTime: res.data.latestAlertTime,
                  alertType: null,
                  id: null,
                };

                useAlertDetailStore().SetMessage(alertMessage);
                addDialog({
                  contentRenderer: ({ options, index }) => (
                    <alertDialog closeFn={testclose(options, index)}>
                      {" "}
                    </alertDialog>
                  ),
                  class: "alertDialog",
                  headerRenderer: ({ close, titleId, titleClass }) => (
                    // jsx 语法
                    <reHeader
                      headerId={titleId}
                      headerClass={titleClass}
                      closeFn={close}
                      headerTitle={"处理告警"}
                    >
                      {" "}
                    </reHeader>
                  ),
                  hideFooter: true,
                });
              });
            });

            instance.close();
          }
        },
      },
      options
    );
    instance = ElNotification(mergeOptions);
  }, 0);
};
